using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace PlayTime
{
    public class BridgePlayer:Player
    {
        public const int nHnadSize = 13;
        private Position m_ePosition;
       
        private ArrayList m_cCallList;
        private ArrayList m_cCardAtHand;
        private ArrayList[] m_cCardAtHandBySuit;
        private CardSuit m_eMaxSuit;
        private int  m_nMaxSuitLength;
        public BridgePlayer(Position pos,string name):base(name)
        {
            m_ePosition = pos;
            m_cCardAtHand = new ArrayList(nHnadSize);
            m_cCallList = new ArrayList();
            m_cCardAtHandBySuit = new ArrayList[4];
            m_cCardAtHandBySuit[0] = new ArrayList();
            m_cCardAtHandBySuit[1] = new ArrayList();
            m_cCardAtHandBySuit[2] = new ArrayList();
            m_cCardAtHandBySuit[3] = new ArrayList();
        }
        public Position Position
        {
            get { return m_ePosition; }
            set { m_ePosition = value; }
        }
        internal void DealCards(Session session , Hand hand)
        {
            session.Deck.Shuffle();
            DistributeCard(session.Deck, hand);

        }
        private void DistributeCard(Deck rCardPack, Hand hand)
        {            
            int nPackeSize = rCardPack.getPackSize();
            Position pos = hand.Session.Dealer.Position;
            for (int i = 0; i < nPackeSize; i++)
            {
                Card draw = rCardPack.GetCardFromDeck();
                BridgePlayer player = hand.Session.getPlayer(pos);
                player.AddCardToHand(draw); 
                pos = hand.getNextPostion(pos);   
            }

//#else
//            Position pos = hand.Session.Dealer.Position;
           
//            for (int np = 0; np < 4; np++)
//            {
//                BridgePlayer player = hand.Session.getPlayer(pos);
//                for (int i = 0; i < 13; i++)
//                {
//                   Card draw = rCardPack.GetCardFromDeck();
//                   player.AddCardToHand(draw);
//               }
//                pos = hand.getNextPostion(pos);
//           }
//#endif
           
        }

        public void AddCardToHand(Card draw)
        {
            m_cCardAtHand.Add(draw);
            CardSuit cs = draw.CardSuit;
            m_cCardAtHandBySuit[(int)cs].Add(draw);
            UpdateSuits(cs);
        }

        private void UpdateSuits(CardSuit cs)
        {
            ArrayList arr = m_cCardAtHandBySuit[(int)cs];            
            CardSuit higSuit = cs;
            int nCurSuitLen;
            int nCurMaxSuitLen;
            nCurMaxSuitLen = nCurSuitLen = arr.Count;
            for (int i = 0; i < 4; i++)
            {
                arr = m_cCardAtHandBySuit[i];
                nCurSuitLen =  arr.Count;             
                if (nCurSuitLen > nCurMaxSuitLen)
                {
                    nCurMaxSuitLen = nCurSuitLen;
                    higSuit = (CardSuit)i;
                }
            }
            m_nMaxSuitLength = nCurMaxSuitLen;
            m_eMaxSuit = higSuit ;
        }
        public int MaxSuitLength
        {
            get { return m_nMaxSuitLength; }
        }
        public Card GetBackCard()
        {
            Card acard = (Card)m_cCardAtHand[0];
            m_cCardAtHand.RemoveAt(0);
            return acard;
        }

        internal void ArrangeCard()
        {
        }

        internal Card[] GetHand()
        {
            Card[] cardList = new Card[m_cCardAtHand.Count];
            for (int i = 0; i < m_cCardAtHand.Count; i++)
            {
                cardList[i] = (Card)m_cCardAtHand[i];
            }
            return cardList;
        }

        internal void InsertBid(Call curCall)
        {
            m_cCallList.Add(curCall);
        }

        internal bool IsOpponent(Position position)
        {
            bool bRet = false;
            switch (m_ePosition)
            {
                case Position.East:
                case Position.West:
                    if((position == Position.North)||(position == Position.South))
                    {
                        bRet = true;
                       
                    }
                    break;
                case Position.South:
                case Position.North:
                    if((position == Position.East)||(position == Position.West))
                    {
                        bRet = true;
                       
                    }
                    break;
            }
            return bRet;
        }

        internal void Lead(Card card)
        {
            Card handCard;
            for (int i = 0; i < m_cCardAtHand.Count; i++)
            {
                handCard = (Card)m_cCardAtHand[i];

                if ((card.CardSuit == handCard.CardSuit)&&(card.CardType == handCard.CardType))
                {
                    m_cCardAtHand.RemoveAt(i);
                    break;
                }
            }
        }
                
        internal bool Contains(CardSuit cardSuit)
        {
            bool bContains = false;            
            for (int i = 0; i < m_cCardAtHand.Count; i++)
            {
                Card card = (Card)m_cCardAtHand[i];
                if (card.CardSuit == cardSuit)
                {
                    bContains = true;
                    break;
                }
            }
            return bContains;
        }

        internal void AddTrick(Trick Trick)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}
